Skip to content

fix(allocations): stop the heap sampling when getting profile#357

Merged
szegedi merged 1 commit into
mainfrom
ishabi/allocation-cumulative-samples
Jun 25, 2026
Merged

fix(allocations): stop the heap sampling when getting profile#357
szegedi merged 1 commit into
mainfrom
ishabi/allocation-cumulative-samples

Conversation

@IlyasShabi

Copy link
Copy Markdown

What does this PR do?:

In ts/src/heap-profiler.ts, after each profile() call in allocation mode, stop and immediately restart the V8 sampling heap profiler with the original parameters. The stop call frees V8's sample buffer; the restart resumes sampling for the next window.

Motivation:

When the heap profiler is started with allocations enabled, V8 retains a record of every sampled allocation. The only V8 API that frees that buffer is StopSamplingHeapProfiler and GetAllocationProfile does not clear it.

Today the binding never restarts the sampler between collections. Each call to profile() re-aggregates the entire history of sampled allocations since the profiler was started, producing two issues:

  1. Reported alloc_* totals grow ~N× across N consecutive profile() calls instead of reflecting the per-window allocation rate.
  2. V8's internal sample buffer grows for the lifetime of the process (memory leak)

Additional Notes:
Benchmark

Baseline

  #  pprof_samples   alloc_MiB   inuse_MiB     alloc_objects   ratio_vs_#1
   1              8    5589.60       1.93        366117116     1.00x
   2             10   11202.27       2.43        733927295     2.00x
   3             11   16864.94       2.43       1104955079     3.02x
   4             11   22508.62       2.43       1474818782     4.03x
   5             13   28424.36       2.49       1862485415     5.09x

With the Fix

  #  pprof_samples   alloc_MiB   inuse_MiB     alloc_objects   ratio_vs_#1
   1              6    5656.60       1.93        370557315     1.00x
   2              6    5635.10       2.43        369125338     1.00x
   3              4    5625.60       2.43        368536560     0.99x
   4              5    5589.10       1.93        366109674     0.99x
   5              7    5893.67       1.99        386073436     1.04x

@IlyasShabi IlyasShabi added the semver-patch Bug or security fixes, mainly label Jun 25, 2026
@github-actions

Copy link
Copy Markdown

Overall package size

Self size: 2.19 MB
Deduped: 2.89 MB
No deduping: 2.89 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | pprof-format | 2.2.2 | 500.53 kB | 500.53 kB | | source-map | 0.7.6 | 185.63 kB | 185.63 kB | | node-gyp-build | 4.8.4 | 13.86 kB | 13.86 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-datadog-prod-us1-2

Copy link
Copy Markdown

Pipelines

Fix all issues with BitsAI

⚠️ Warnings

🚦 1 Pipeline job failed

Pull Request Labels | label   View in Datadog   GitHub Actions

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 42ffb02 | Docs | Datadog PR Page | Give us feedback!

@IlyasShabi IlyasShabi marked this pull request as ready for review June 25, 2026 15:11
@szegedi szegedi merged commit 2715d97 into main Jun 25, 2026
70 of 71 checks passed
@szegedi szegedi deleted the ishabi/allocation-cumulative-samples branch June 25, 2026 15:30
@IlyasShabi IlyasShabi mentioned this pull request Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-patch Bug or security fixes, mainly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants